<?php
class LoginController extends Zend_Controller_Action
{
	private $_strKeyLogin	= '_ses_login';
	private $_intMaxTime	= 3;
	
	public function init(){}
	
	public function indexAction()
	{
		if ($this->_request->isPost())
		{
			$AppUI			= Zend_Registry::get('AppUI');
			$intError		= $AppUI->_get($this->_strKeyLogin, 0);
			
			$strUsername 	= $this->_request->getParam('username','');
			$strPassword	= $this->_request->getParam('password','');
			$strCountry		= $this->_request->getParam('country','');
			
			$this->view->intCaptcha = ($intError>=$this->_intMaxTime) ? 1: 0;
			
			if ($strUsername && $strPassword)
			{
				if($intError>=$this->_intMaxTime)
				{
					$strCaptcha	= $this->_request->getParam('strCaptcha','');
					if( !$AppUI->checkCaptcha($strCaptcha) )
					{
						$this->view->strUsername	= $strUsername;
						$this->view->strPassword	= $strPassword;
						$this->view->intErrCaptcha	= 1;
						$bolCheckCaptcha			= false;
						return;
					}
				}
				$bolCheck = CAdminLogin::checkLogin($strUsername, $strPassword, $strCountry);
				if ($bolCheck)
				{
					Common_FCommon::redirect(HOST_ADMIN);
					exit;
				}
				else
				{
					$intError++;
					$AppUI->_add($this->_strKeyLogin, $intError);
					
					$this->view->error 			= true;
				}
			}
		}
		
		
		$AppUI			= Zend_Registry::get('AppUI');
		$strCountry		= $this->_request->getParam('country','');
       
		if($AppUI->admin_id !=NULL && $AppUI->admin_group_id !=NULL && $strCountry == $AppUI->country)
		{
			Common_FCommon::redirect(HOST_ADMIN);
			exit;
		}

		$this->view->arrCountry	= CConfig::country_list();
		$this->view->selectedCountry = $strCountry;
	}
	
	public function logoutAction()
	{
		CAdminLogin::doLogout();
		$this->_redirect(HOST_ADMIN);
	}
}
?>